//
//  WXPProductPackageFooterView.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/15.
//

#import "WXPProductPackageFooterView.h"

@implementation WXPProductPackageFooterView

#pragma mark - lazy
- (UIView *)viewTop{
    if (_viewTop == nil) {
        _viewTop = [UILabel new];
        _viewTop.backgroundColor = COLOR_WHITE;
        _viewTop.widthHeight = XY(SCREEN_WIDTH-LEFT_MARDIN*2, 0);
        _viewTop.userInteractionEnabled = true;
    }
    return _viewTop;
}
- (UILabel *)labelContent{
    if (_labelContent == nil) {
        _labelContent = [UILabel  new];
        _labelContent.textColor = COLOR_RED;
        _labelContent.fontNum = 12;
        _labelContent.numberOfLines = 0;
        _labelContent.lineSpace = 3;
        [_labelContent fitTitle:@"        Presiona el botón para solicitar la revisión. Una vez aprobada la revisión, el préstamo se depositará automáticamente en la cuenta bancaria vinculada" variable:SCREEN_WIDTH-LEFT_MARDIN*4];
    }
    return  _labelContent;
}
- (UIButton *)btnTotalLoan
{
    if (_btnTotalLoan == nil) {
        _btnTotalLoan = [UIButton  new];
        _btnTotalLoan.titleLabel.fontNum = 12;
        [_btnTotalLoan setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        [_btnTotalLoan setTitle:@"Ver los detalles del préstamo>" forState:UIControlStateNormal];
        [_btnTotalLoan sizeToFit];
        _btnTotalLoan.widthHeight = XY(_btnTotalLoan.width+LEFT_MARDIN*2, _btnTotalLoan.height+20);
        _btnTotalLoan.tag = 1;
        [_btnTotalLoan addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _btnTotalLoan;
}
- (UIButton *)btnAgreement
{
    if (_btnAgreement == nil) {
        _btnAgreement = [UIButton  new];
        [_btnAgreement setImage:[UIImage imageNamed:@"icon-agreement-select"] forState:UIControlStateNormal];
        [_btnAgreement setImage:[UIImage imageNamed:@"icon-agreement-select"] forState:UIControlStateSelected];
        _btnAgreement.widthHeight = XY(14+12+12, 14+12+12);
        _btnAgreement.tag = 2;
        [_btnAgreement addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _btnAgreement;
}
- (NSString *)strAgreement{
    if (!_strAgreement) {
        _strAgreement = [NSString new];
        _strAgreement = @"Estoy de acuerdo con el 《acuerdo de préstamo》 después de leerlo";
    }
    return _strAgreement;
}
- (NSString *)strAgreementMark{
    if (!_strAgreementMark) {
        _strAgreementMark = [NSString new];
        _strAgreementMark = @"《acuerdo de préstamo》";
    }
    return _strAgreementMark;
}
- (UITextView *)labelAgreement
{
    if (_labelAgreement == nil) {
        _labelAgreement = [UITextView  new];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.strAgreement];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 2;
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedString.length)];
        [attributedString addAttribute:NSLinkAttributeName
                                 value:@"agreement"
                                 range:[[attributedString string] rangeOfString:self.strAgreementMark]];
        [attributedString addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]} range:[self.strAgreement rangeOfString:self.strAgreementMark]];
        _labelAgreement.attributedText = attributedString;
        _labelAgreement.linkTextAttributes = @{NSForegroundColorAttributeName: COLOR_BLUE,
                                               NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
       
        UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 0, 0);
        _labelAgreement.textContainerInset = insets;
        _labelAgreement.width = SCREEN_WIDTH-LEFT_MARDIN-3-self.btnAgreement.width;
        _labelAgreement.font = [UIFont systemFontOfSize:12];
        _labelAgreement.textAlignment = NSTextAlignmentLeft;
        [_labelAgreement sizeToFit];
        _labelAgreement.textColor = COLOR_LIGHT_GRAY;
        _labelAgreement.backgroundColor = [UIColor clearColor];
        _labelAgreement.delegate = self;
        _labelAgreement.editable = NO;
        _labelAgreement.scrollEnabled = NO;
    }
    return  _labelAgreement;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.widthHeight = XY(SCREEN_WIDTH, 0);
        self.backgroundColor = [UIColor clearColor];
        [self addSubView];
    }
    return self;
}

- (void)addSubView{
    [self addSubview:self.viewTop];
    [self.viewTop addSubview:self.labelContent];
    [self.viewTop addSubview:self.btnTotalLoan];
    [self addSubview:self.btnAgreement];
    [self addSubview:self.labelAgreement];
    [self resetView];
}

#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    [NSObject clickFeedback];
    [sender addClickAnimate];
    switch (sender.tag) {
        case 1:
        {
            if(self.blockJumpToTotalLoanInfo){
                self.blockJumpToTotalLoanInfo();
            }
        }
            break;
        default:
            break;
    }
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(nonnull NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction{
    [NSObject clickFeedback];
    NSRange rangeServicio = [self.strAgreement rangeOfString:self.strAgreementMark];
    if (NSEqualRanges(characterRange, rangeServicio)) {
        if(self.blockJumpToAgreement){
            self.blockJumpToAgreement();
        }
    }
    return YES;
}

#pragma mark - 刷新View
- (void)resetView{
    self.labelContent.leftTop = XY(LEFT_MARDIN, 0);
    
    self.btnTotalLoan.rightTop = XY(self.viewTop.width, self.labelContent.bottom);
    
    self.viewTop.height = self.btnTotalLoan.bottom+5;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.viewTop.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(16, 16)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = path.CGPath;
    self.viewTop.layer.mask = maskLayer;
    
    self.viewTop.centerXTop = XY(self.width/2, 0);
    
    self.btnAgreement.leftTop = XY(3, self.viewTop.bottom+12);
    self.labelAgreement.leftTop = XY(self.btnAgreement.right, self.btnAgreement.top+11);
    
    self.height = self.labelAgreement.bottom+LEFT_MARDIN;
}

@end
